# File contains tools for "Not A Question" post text quantification
# TODO Extract code from post text

import re
from Resources.config import NOT_A_AQUESTION_QUANTIFIER_SPLIT
from Resources.question_regexps import REGEXP_LIST

# Will return true if question mark present within the text
def questionMarkPresent(text):
	return "?" in text

_5w1H = { "what", "when", "where", "who", "why", "how" }

# Will return true if element of _5W1H is present within the text
def _5W1H_Present(text):
	
	present = False
	text_low = text.lower()
	
	for word in _5w1H:
		if word in text_low:
			present = True

	return present

# Will return tru if text matches one of paterns from REGEXP_LIST
def regexpMatch(text):

	match = False

	for pattern in REGEXP_LIST:
		if re.match(pattern, text, re.M|re.I):
			match = True

	return match

# Will return sum of eligible parts of quantifier split
def quantify(text):
	result = 0

	if questionMarkPresent(text):
		result += NOT_A_AQUESTION_QUANTIFIER_SPLIT['QUESTION_MARK_PRESENT']

	if _5W1H_Present(text):
		result += NOT_A_AQUESTION_QUANTIFIER_SPLIT['5W1H_PRESENT']

	if regexpMatch(text):
		result += NOT_A_AQUESTION_QUANTIFIER_SPLIT['REGEXP_MATCH']
	
	return result 

# Everything below is for testing purposes only.
"""
import Parser
import Post
import FileIO

fileStream = FileIO.openTrainingFile(FileIO.READ_MODE)
Parser.readHeader(fileStream)

withQM = 0
withoutQM = 0

with_5W1H = 0
without_5W1H = 0

matched_regexp = 0
unmatched_regexp = 0

overall_quantified_val_acc = 0
counter = 0

open_quantified_val_acc = 0
open_counter = 0

closed_quantified_val_acc = 0
closed_counter = 0

# NQ = Not a question
NQ_quantified_val_acc = 0
NQ_counter = 0

while (not Parser.isEndOfFile(fileStream)):
	
	quantified_val = 0
	newPost = Parser.readPost(fileStream)
	
	bodyMarkdown = newPost.bodyMarkdown

	QM = questionMarkPresent(bodyMarkdown)
	_5w1H_match = _5W1H_Present(bodyMarkdown);
	regexp_match = regexpMatch(bodyMarkdown)

	if QM:
		withQM += 1
		quantified_val += NOT_A_AQUESTION_QUANTIFIER_SPLIT['QUESTION_MARK_PRESENT']
	else:
		withoutQM += 1

	if _5w1H_match:
		with_5W1H += 1
		quantified_val += NOT_A_AQUESTION_QUANTIFIER_SPLIT['5W1H_PRESENT']
	else:
		without_5W1H += 1

	if regexp_match:
		matched_regexp += 1
		quantified_val += NOT_A_AQUESTION_QUANTIFIER_SPLIT['REGEXP_MATCH']
	else:
		unmatched_regexp += 1

	overall_quantified_val_acc += quantified_val
	counter += 1

	post_status = Post.getFullPostStatus( newPost.openStatus )

	if ( post_status == "open"):
		open_quantified_val_acc += quantified_val
		open_counter += 1
	else:
		closed_quantified_val_acc += quantified_val
		closed_counter += 1

	if ( post_status == "not a real question"):
		NQ_quantified_val_acc += quantified_val
		NQ_counter += 1 

	print "Post - ", counter, "; QM - ", QM, "; 5W1H - ", _5w1H_match, "; regexp match - ", regexp_match, "; Post status - \'" + post_status + "\'"

import datetime
f = open("Output/NQ_quantifier_" + datetime.datetime.now().strftime("%Y%m%d_%H%M%S") + ".log", 'w')

f.write( "Current quantifier config:\n\nNOT_A_AQUESTION_QUANTIFIER_SPLIT = {")

for key in NOT_A_AQUESTION_QUANTIFIER_SPLIT:
	f.write( "\n\t\'" + str(key) + "\' : " + str(NOT_A_AQUESTION_QUANTIFIER_SPLIT[key]))

f.write( "\n}\n\nAll posts:                                    " + str(counter) )
f.write( "\n\nPosts with question mark:                      " + str(withQM) )
f.write( "\nPosts without question mark:                   " + str(withoutQM) )
f.write( "\n\nPosts with 5W1H:                               " + str(with_5W1H) )
f.write( "\nPosts without 5W1H:                            " + str(without_5W1H) )
f.write( "\n\nPosts matched question regexp:                 " + str(matched_regexp) )
f.write( "\nPosts unmatched question regexp:               " + str(unmatched_regexp) )
f.write( "\n\nAverage NQ quantifier value over all posts:    " + str(float(overall_quantified_val_acc)/counter) )
f.write( "\nAverage NQ quantifier value over open posts:   " + str(float(open_quantified_val_acc)/open_counter) )
f.write( "\nAverage NQ quantifier value over closed posts: " + str(float(closed_quantified_val_acc)/closed_counter) )
f.write( "\nAverage NQ quantifier value over NQ posts:     " + str(float(NQ_quantified_val_acc)/NQ_counter) )

f.close()
"""